<?php

namespace App\Http\Controllers\Api\CatchOrder;

use App\Http\Controllers\Api\BaseController;
use App\Jobs\OrderSubmit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;

class CatchKwaiOrderController extends BaseController
{
    public $log;

    public function __construct()
    {
        $this->log = new Logger('kwaiPushCatchData');
        $this->log->pushHandler(
            (new RotatingFileHandler(storage_path('/logs/kwaiPushCatchData.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
        );
    }

    //
    public function pushOrder(Request $Request)
    {
        $getData = file_get_contents('php://input');
        $this->log->info('input' . ($getData));
        // $getData = '{"appKey":"ks649644249502195770","info":{
        //     "oid": "aa123456798",
        //     "sellerId": 123,
        //     "openId": "xjfdaowqe123213",
        //     "status": 3,
        //     "beforeStatus": 80,
        //     "updateTime": 123
        //   }}';

        $getDatas = json_decode($getData, true);
        // var_dump($getDatas, 111111111);
        $shop_id = 0;
        $pid = '';
        if (!empty($getDatas)) {
            foreach ($getDatas as $key => $v) {
                // var_dump($v, 55555555);
                if ($key == 'info' && !empty($v)) {
                    if (is_string($v)) {
                        $arrays = json_decode($v, true);
                    } else {
                        $arrays = $v;
                    }

                    // var_dump($arrays, 66666666);
                    // 判断下状态
                    $flag = true;
                    if (!empty($arrays['status'])) {
                        switch ($arrays['status']) {
                            case 0:
                                // 未知
                                $flag = false;
                                break;
                            case 1:
                                // 全部
                                $flag = false;
                                break;
                            case 2:
                                // 代付款
                                $flag = false;
                                break;
                            case 3:
                                //待发货
                                $flag = true;
                                break;
                            case 4:
                                //待收货
                                $flag = false;
                                break;
                            case 5:
                                //已收货
                                $flag = false;
                                break;
                            case 6:
                                //交易成功
                                $flag = false;
                                break;
                            case 7:
                                //已关闭订单
                                $flag = false;
                                break;
                        }
                    }
                    // 备货中 和 待确认单 暂不处理
                    if (!$flag) {
                        continue;
                    }
                    $shop_key = !empty($getDatas['appKey']) ? $getDatas['appKey'] : '';
                    $pid = !empty($arrays['oid']) ? $arrays['oid'] : '';
                    if (!empty($shop_key) && !empty($pid)) {
                        $data = [
                            'app_key' => $shop_key,
                            'pid' => $pid,
                            'type' => 'kwai'
                        ];
                        // 将数据存入到表中   防止遗漏 验证
                        $res = DB::table('kwai_catch_verific')->where(['pid' => $pid])->first();
                        if (!$res) {
                            $datas = [
                                'pid' => $pid,
                                'shop_id' => $shop_key,
                                'created_at' => date('Y-m-d H:i:s'),
                                'is_ver' => 1
                            ];
                            DB::table('kwai_catch_verific')->insert($datas);
                            // var_dump($data);
                            dispatch(new OrderSubmit($data, 'catchOrder', 60))->onQueue('TaskOrderSubmitDispose');
                        }
                    }
                }
            }
        }
        $str = '{"result":1}';
        $this->log->info('输入的数据' . $getData . '返回的参数' . $str);
        return $str;
    }

    public function upThree($ordernum = '', $type = 'catchOrder')
    {
        dispatch(new OrderSubmit($ordernum, $type))->onQueue('TaskOrderSubmitDispose');
        return 1;
    }
}
